package com.cxhc.system.service.app.impl;

import com.cxhc.system.domain.app.Examination;
import com.cxhc.system.domain.app.ExaminationQuestion;
import com.cxhc.system.domain.app.ExaminationUserQuestion;
import com.cxhc.system.mapper.app.ExaminationAppMapper;
import com.cxhc.system.mapper.app.ExaminationUserQuestionMapper;
import com.cxhc.system.service.app.IExaminationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ExaminationAppServiceImpl implements IExaminationService {

    @Autowired
    private ExaminationAppMapper examinationAppMapper;

    @Autowired
    private ExaminationUserQuestionMapper examinationUserQuestionMapper;

    @Override
    public List<Examination> getAllExamination(Examination examinationApp) {

        return examinationAppMapper.selectExaminationList(examinationApp);

    }



    @Override
    public int submit(List<ExaminationUserQuestion> questionsAnswer) {
        return examinationUserQuestionMapper.batchInsert(questionsAnswer);
    }

    @Override
    public float calculateScore(String userId,String examinationId) {
        float score = 0;
        //用考试ID查询出考题
        Map<Long, ExaminationQuestion> longExaminationQuestionMap =
                examinationAppMapper.selectExaminationQuestionMap(examinationId);

        //获取用户该场次考题
        List<ExaminationUserQuestion> examinationUserQuestions =
                examinationUserQuestionMapper.selectByUserAndExamination(userId, examinationId);

        for (ExaminationUserQuestion examinationUserQuestion : examinationUserQuestions) {
            Long questionId = examinationUserQuestion.getQuestionId();
            ExaminationQuestion question = longExaminationQuestionMap.get(questionId);
            if (question.getAnswer() == examinationUserQuestion.getAnswer()){
                score += question.getScore();
            }
        }
        return score;
    }

    @Override
    public List<Examination> queryExaminationByUserId(String userId) {
        return examinationAppMapper.selectByUserId(userId);
    }
}
